package controllers

import (
	"strings"

	"gitee.com/tsingeye/FreeEhome/models"
)

/**
 * @apiDefine device 设备接口
 */
type DeviceController struct {
	AuthController
}

/**
 * @api {get} /api/v1/devices 查询设备列表
 * @apiVersion 1.0.0
 * @apiGroup device
 * @apiName DeviceList
 * @apiParam {String} [status] 设备状态 ON-在线；OFF-离线；默认查询全部
 * @apiParam {Number} [page] 页码，分页时默认从1开始
 * @apiParam {Number} [limit] 分页大小，默认10
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "errCode": 200,
 *   "errMsg": "OK",
 *   "totalCount": 100, //总数
 *   "deviceList": [
 *     {
 *       "deviceID": "wyd123", //设备ID
 *       "deviceLocalIP": "127.0.0.1", //设备IP
 *       "serialNumber": "wyd666", //设备序列号
 *       "status": "ON" //设备状态：ON-在线；OFF-离线
 *       "createdAt": "2020-10-20 10:20:10", //创建时间
 *       "updatedAt": "2020-10-20 10:20:10" //更新时间
 *     }
 *    ]
 * }
 */
func (this *DeviceController) DeviceList() {
	//设备状态
	status := strings.ToUpper(this.GetString("status"))

	//页码，可选，分页时默认从1开始
	page, err := this.GetInt("page")
	if err != nil || page <= 0 {
		page = 1
	}

	//分页大小，可选，默认100
	limit, err := this.GetInt("limit")
	if err != nil || limit <= 0 {
		limit = 10
	}

	this.Data["json"] = models.DeviceList(status, page, limit)
	this.ServeJSON()
}

/**
 * @api {get} /api/v1/devices/:deviceID/channels 查询指定设备下的通道列表
 * @apiVersion 1.0.0
 * @apiGroup device
 * @apiName ChannelList
 * @apiParam {String} [status] 通道状态 ON-在线；OFF-离线；默认查询全部
 * @apiParam {Number} [page] 页码，分页时默认从1开始
 * @apiParam {Number} [limit] 分页大小，默认10
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "errCode": 200,
 *   "errMsg": "OK",
 *   "totalCount": 100, //总数
 *   "channelList": [
 *     {
 *       "channelID": "123456", //设备通道ID
 *       "channelName": "wyd", //设备通道名称
 *       "channelAlias": "dyw", //通道别名
 *       "deviceID": "wyd123", //设备ID
 *       "status": "ON" //通道状态：ON-在线；OFF-离线
 *       "createdAt": "2020-10-20 10:20:10", //创建时间
 *       "updatedAt": "2020-10-20 10:20:10" //更新时间
 *     }
 *    ]
 * }
 */
func (this *DeviceController) ChannelList() {
	//设备ID
	deviceID := this.Ctx.Input.Param(":deviceID")

	//通道状态
	status := strings.ToUpper(this.GetString("status"))

	//页码，可选，分页时默认从1开始
	page, err := this.GetInt("page")
	if err != nil || page <= 0 {
		page = 1
	}

	//分页大小，可选，默认100
	limit, err := this.GetInt("limit")
	if err != nil || limit <= 0 {
		limit = 10
	}

	this.Data["json"] = models.ChannelList(deviceID, status, page, limit)
	this.ServeJSON()
}

/**
 * @api {patch} /api/v1/channels/:channelID 更新通道
 * @apiGroup device
 * @apiName UpdateChannel
 * @apiParamExample {json} Request-Example:
 * {
 *   "channelAlias": "dyw", //通道别名
 * }
 * @apiSuccessExample {json} Success-Response:
 * {
 *   "errCode": 200,
 *   "errMsg": "Success OK"
 * }
 */
func (this *DeviceController) UpdateChannel() {
	//通道ID
	channelID := this.Ctx.Input.Param(":channelID")

	this.Data["json"] = models.UpdateChannel(channelID, this.Ctx.Input.RequestBody, this.Ctx.Request.RemoteAddr)
	this.ServeJSON()
}

/**
 * @api {get} /api/v1/channels/:channelID/ptzControl 云台控制
 * @apiVersion 1.0.0
 * @apiGroup device
 * @apiName PtzControl
 * @apiParam {String}  ptzCmd LEFT、RIHGT、UP、DOWN
 * @apiParam {String} action Start、Stop
 * @apiParam {Number} speed 1-7
 */
func (this *DeviceController) PtzControl() {
	//通道ID
	channelID := this.Ctx.Input.Param(":channelID")
	//控制指令
	ptzCmd := this.GetString("ptzCmd")
	//开始时间
	action := this.GetString("action")

	//速度类型
	speed, err := this.GetInt64("speed")
	if err != nil {
		//默认速度
		speed = 3
	}

	this.Data["json"] = models.PtzControl(channelID, ptzCmd, action, speed)
	this.ServeJSON()
}
